package com.linkage.bss.crm.packagemgr.util;

import java.lang.reflect.Method;

import com.linkage.bss.crm.packagemgr.model.ActionOrderItem;
import com.linkage.bss.crm.packagemgr.model.PackageObject;

public class BeanToJS {

	public static String bean2js(Class cls) {
		StringBuffer js = new StringBuffer();
		js.append("function ");
		js.append(cls.getSimpleName());
		js.append("()");
		js.append("{ ");

		Method[] met = cls.getMethods();
		for (int t = 0; met != null && t < met.length; t++) {
			String name = met[t].getName();

			if (name.startsWith("getClass")) {
				continue;
			}

			if (name.startsWith("get")) {
				js.append(cls.getSimpleName());
				js.append(".");
				js.append("prototype").append(".");
				String subName = name.substring(3, 4).toLowerCase() + name.substring(4, name.length());

				js.append(subName + ";");

			}
		}

		for (int t = 0; met != null && t < met.length; t++) {
			String name = met[t].getName();
			if (name.startsWith("getClass")) {
				continue;
			}
			if (name.startsWith("get")) {

				String subNameU = name.substring(3, 4).toUpperCase() + name.substring(4, name.length());
				String subNameL = name.substring(3, 4).toLowerCase() + name.substring(4, name.length());

				// .prototype.set

				js.append(cls.getSimpleName());
				js.append(".");
				js.append("prototype").append(".");
				js.append("set");
				js.append(subNameU);
				js.append(" = function (");
				js.append(subNameL);
				js.append(" ) { this." + subNameL + " = " + subNameL + " ; } ");

				// prototype.get
				js.append(cls.getSimpleName());
				js.append(".");
				js.append("prototype").append(".");
				js.append("get");
				js.append(subNameU);
				js.append(" = function ()");

				js.append("  { return this." + subNameL + " ; } ");

			}
		}
		js.append("};");
		return js.toString();
	}

	public static String po2vo(Class clazz) {

		StringBuffer vo = new StringBuffer();

		Method[] met = clazz.getMethods();
		for (int t = 0; met != null && t < met.length; t++) {
			String name = met[t].getName();

			if (name.startsWith("getClass")) {
				continue;
			}

			if (name.startsWith("set")) {

				//this.BaseInfoVo.obj.getreaId;this.BaseInfoVo.obj.getreaName
				vo.append("this");
				vo.append(".");
				vo.append(name.toString());
				vo.append("(");
				vo.append("obj").append(".get");
				String subName = name.substring(3, name.length());
				vo.append(subName + "());  ");

			}
		}
		return vo.toString();

	}

	public static String po2js(Class clazz, String qz, String objName) {

		StringBuffer vo = new StringBuffer();

		Method[] met = clazz.getMethods();
		for (int t = 0; met != null && t < met.length; t++) {
			String name = met[t].getName();

			if (name.startsWith("getClass")) {
				continue;
			}

			if (name.startsWith("set")) {

				//this.BaseInfoVo.obj.getreaId;this.BaseInfoVo.obj.getreaName
				vo.append(qz);
				vo.append(".");
				vo.append(name.toString());
				vo.append("(");
				vo.append(objName).append(".");
				String subName = name.substring(3, 4).toLowerCase() + name.substring(4, name.length());
				vo.append(subName + ");  ");

			}
		}
		return vo.toString();

	}

	public static void main(String[] args) {

		//		System.out.println(BeanToJS.bean2js(BaseInfoVo.class));
		//		System.out.println(BeanToJS.bean2js(PartobjVo.class));
		//		System.out.println(BeanToJS.bean2js(ActionOredrItemVo.class));
		//		System.out.println(BeanToJS.bean2js(PackageParameterVo.class));
		//		System.out.println(BeanToJS.bean2js(ActionParameterVo.class));

		//		System.out.println(BeanToJS.po2vo(BaseInfoVo.class));
		//		System.out.println(BeanToJS.po2vo(PartobjVo.class));
		//		System.out.println(BeanToJS.po2vo(ActionOredrItemVo.class));
		//		System.out.println(BeanToJS.po2vo(PackageParameterVo.class));
		//		System.out.println(BeanToJS.po2vo(ActionParameterVo.class));

		//	System.out.println(BeanToJS.po2js(BaseInfoVo.class, "baseInfo", "obj"));
		System.out.println(BeanToJS.po2js(PackageObject.class, "pObj", "obj"));
		System.out.println(BeanToJS.po2js(ActionOrderItem.class, "item", "obj"));

	}
}
